﻿using UnityEngine;
using System.Collections.Generic;
public static class Collider2DExtensions
{
    public static float GetArea(this Collider2D collider)
    {
        if(collider is BoxCollider2D)
        {
            var siz = (collider as BoxCollider2D).size;
            return siz.x * siz.y;
        }else if(collider is CircleCollider2D)
        {
            var r = (collider as CircleCollider2D).radius;
            return Mathf.PI * r * r;
        }else if(collider is PolygonCollider2D)
        {
;           return (collider as PolygonCollider2D).GetPolygonColliderArea();
        }
        else
        {
            Debug.LogError($"{collider.GetType()}:没有为此类型编写计算面积的函数");
            return 0;
        }

    }
    public static void ChangeLayer(this GameObject item,LayerMask layer)
    {
        item.layer = layer;
        for(int i = 0; i < item.transform.childCount; i++)
        {
            item.transform.GetChild(i).gameObject.ChangeLayer(layer);
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="collider"></param>
    /// <param name="focusLayer">仅与Item脚本所挂载物体的layer有关</param>
    /// <returns></returns>
    public static List<T> GetInColliderParent<T>(this Collider2D collider,int focusLayer=-1)where T:MonoBehaviour
    {

        List<Collider2D> ans = new List<Collider2D>();

        var filter = new ContactFilter2D();
        filter.useTriggers = false;
        filter.SetLayerMask(Physics2D.GetLayerCollisionMask(0));
        filter.useLayerMask = true;
        //filter.SetLayerMask(collider.gameObject.layer);
     //   filter.useLayerMask = true;
   //     filter.layerMask = collider.gameObject.layer;
        filter.useTriggers = false;
        Physics2D.OverlapCollider(collider,filter, ans);
        var list = new List<T>();
        foreach(var x in ans)
        {
            /*   if (focusLayer!=-1&& x.gameObject.layer !=focusLayer)
               {
                   continue;
               }*/
            if (focusLayer != -1)
            {
                var item = x.GetItem();
                if (item == null)
                {
                    continue;
                }else if (item.gameObject.layer != focusLayer)
                {
                    continue;
                }
            }
            if (x.GetComponentsInParent < T >().Length>0)
            {
                var coms = x.GetComponentsInParent<T>();
             /*   bool flag = true;

                foreach(var c in coms)
                {
                    if (focusLayer!=-1&& c.gameObject.layer != focusLayer)
                    {
                        flag = false;
                    }
                }
                if (!flag)
                {
                    continue;
                }*/
                foreach(var c in coms)
                {
                    list.UniqueAdd(c);
                }
            }
        }
        return list;
    }
    public static Item GetItem(this Collider2D collider)
    {
        Transform trans = collider.transform;
        while (trans != null && trans.GetComponent<Item>() == null)
        {
            trans = trans.parent;
        }
        if (trans == null)
        {
            return null;
        }
        return trans.GetComponent<Item>();
    }
    public static string GetItemName(this Collider2D collider)
    {
        var item = collider.GetItem();
        if (item != null)
        {
            return item.ItemFieldSetting.ItemName;
        }
        else
        {
            return default;
        }
    }
}